package com.gm.commonlib.base.baseui.baseactivity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.gm.commonlib.R;
import com.gm.commonlib.base.baseBean.BaseFragmentInfo;
import com.gm.commonlib.util.logUtils.LogUtil;
import com.google.android.material.tabs.TabLayout;

import java.util.List;

public abstract class BaseTabLayoutActivity extends BaseActivity {
    public static final String UPDATE_WAYBILL_STATE = "com.update.Waybill.state";
    private static final int DEFAULT_OFFSCREEN_PAGELIMIT = 20;
    public TabLayout mTabLayout;
    public ViewPager mViewPager;
   public ViewPagerAdapter mAdapter;
    public LinearLayout tabLayoutView;
    public LinearLayout headerView;
    private LinearLayout contentHeaderView;
    private LinearLayout footerView;

    public abstract List<BaseFragmentInfo> getFragments();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_tablayout);
        tabLayoutView = findViewById(R.id.tabLayoutView);
        headerView = findViewById(R.id.headerView);
        mViewPager = findViewById(R.id.viewPager);
        contentHeaderView = findViewById(R.id.contentHeaderView);
        footerView=findViewById(R.id.footerView);
        createTabLayout(tabLayoutView);
        createHeaderView(headerView);
        createFootView(footerView);
        createContentHeaderView(contentHeaderView);
        //Tablayout 关联 viewPager
        mTabLayout.setupWithViewPager(mViewPager);
        mAdapter = getAdapter();
        //给ViewPager设置适配器
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOffscreenPageLimit(getOffscreenPageLimit());
        //将TabLayout和ViewPager关联起来
        mTabLayout.setupWithViewPager(mViewPager);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                onPageChecked(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    public void onPageChecked(int postion) {

    }

    public void createTabLayout(LinearLayout tabLayoutView) {
        View view = LayoutInflater.from(this).inflate(R.layout.tablayout, null);
        mTabLayout = view.findViewById(R.id.tabLayout);
        tabLayoutView.addView(view);
    }

    public void createFootView(LinearLayout footerView) {

    }

    public void createHeaderView(LinearLayout headerView) {

    }

    public void createContentHeaderView(LinearLayout contentHeaderView) {

    }


    public int getOffscreenPageLimit() {
        return DEFAULT_OFFSCREEN_PAGELIMIT;
    }


    public ViewPagerAdapter getAdapter() {
        return new ViewPagerAdapter(getSupportFragmentManager(), getFragments());
    }


    public class ViewPagerAdapter extends FragmentStatePagerAdapter {

        private List<BaseFragmentInfo> mFragments;

        public ViewPagerAdapter(FragmentManager fm, List<BaseFragmentInfo> fragments) {
            super(fm);
            mFragments = fragments;
        }

        @Override
        public Fragment getItem(int position) {
            try {
                Fragment fragment1 = mFragments.get(position).getFragment();
                return fragment1;

            } catch (Exception e) {
                LogUtil.d("eeeeeeeeeeeee: " + e.toString());
            }
            return null;
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }


        @Override
        public CharSequence getPageTitle(int position) {
            return mFragments.get(position).getTitle();
        }

        // 动态设置我们标题的方法
        public void setPageTitle(int type, String title) {
            for (BaseFragmentInfo fragmentInfo : mFragments) {
                if (fragmentInfo.getType() == type) {
                    fragmentInfo.setTitle(title);
                    notifyDataSetChanged();
                    break;
                }
            }

        }
    }

    //更新tab标题
    public void updateTabTitle(int type, String title) {
        if (mAdapter != null) {
            mAdapter.setPageTitle(type, title);
        }
    }
}
